package com.childenglish.service;

import com.childenglish.entity.Notification;
import com.childenglish.mapper.NotificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通知服务
 */
@Service
public class NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    /**
     * 创建通知
     */
    public void createNotification(Long userId, String title, String content, String type, String relatedId, String relatedType) {
        Notification notification = new Notification(userId, title, content, type);
        notification.setRelatedId(relatedId);
        notification.setRelatedType(relatedType);
        notificationMapper.insert(notification);
    }

    /**
     * 发送审核通过通知
     */
    public void sendReviewApprovedNotification(Long userId, String fileName, String fileType) {
        String title = "内容审核通过";
        String content = String.format("您上传的%s《%s》已通过审核，现已上线。", 
            "courseware".equals(fileType) ? "课件" : "绘本", fileName);
        createNotification(userId, title, content, "review_approved", fileName, "file");
    }

    /**
     * 发送审核拒绝通知
     */
    public void sendReviewRejectedNotification(Long userId, String fileName, String fileType, String reason) {
        String title = "内容审核未通过";
        String content = String.format("您上传的%s《%s》未通过审核。%s", 
            "courseware".equals(fileType) ? "课件" : "绘本", fileName,
            reason != null && !reason.isEmpty() ? "原因：" + reason : "");
        createNotification(userId, title, content, "review_rejected", fileName, "file");
    }

    /**
     * 获取用户的通知列表
     */
    public List<Notification> getUserNotifications(Long userId) {
        return notificationMapper.selectByUserId(userId);
    }

    /**
     * 获取用户未读通知
     */
    public List<Notification> getUnreadNotifications(Long userId) {
        return notificationMapper.selectUnreadByUserId(userId);
    }

    /**
     * 标记通知为已读
     */
    public void markAsRead(Long notificationId) {
        notificationMapper.markAsRead(notificationId);
    }

    /**
     * 标记所有通知为已读
     */
    public void markAllAsRead(Long userId) {
        notificationMapper.markAllAsRead(userId);
    }

    /**
     * 获取未读通知数量
     */
    public int getUnreadCount(Long userId) {
        return notificationMapper.countUnread(userId);
    }

    /**
     * 发送学习提醒通知
     */
    public void sendLearningReminder(Long userId, String message) {
        String title = "学习提醒";
        createNotification(userId, title, message, "learning_reminder", null, null);
    }

    /**
     * 发送进度通知给家长
     */
    public void sendProgressToParent(Long userId, Long childId, String progressInfo) {
        String title = "学习进度更新";
        String content = String.format("您的孩子学习进度：%s", progressInfo);
        createNotification(userId, title, content, "progress_update", String.valueOf(childId), "child");
    }

    /**
     * 发送系统通知
     */
    public void sendSystemNotification(Long userId, String title, String content) {
        createNotification(userId, title, content, "system", null, null);
    }
}
